package cn.wizzer.app.web.modules.controllers.platform.sys;

import cn.wizzer.app.sys.modules.models.SysCollectBank;
import cn.wizzer.app.sys.modules.services.SysCollectBankService;
import cn.wizzer.app.web.commons.ulog.annotation.SLog;
import cn.wizzer.framework.base.Result;
import cn.wizzer.framework.util.BankCardUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

@IocBean
@At("/platform/sys/bank")
public class SysCollectBankController {

    @Inject
    @Reference
    private SysCollectBankService bankService;

    @At("/")
    @Ok("beetl:/platform/sys/bank/index.html")
    @RequiresPermissions("sys.manager.bank.list")
    public void index() {

    }

    /**
     * 查询银行卡列表（分页）
     * @param searchKeyword String 搜索框输入的内容
     * @param searchDate String[] 时间数组
     * @param pageNumber int
     * @param pageSize int
     * @return Object
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("sys.manager.bank.list")
    public Object data(@Param("searchKeyword") String searchKeyword, @Param("searchDate") String[] searchDate, @Param("pageNumber") int pageNumber, @Param("pageSize") int pageSize) {
        return Result.success().addData(bankService.listPage(pageNumber, pageSize, searchKeyword, searchDate));
    }

    /**
     * 添加Bank
     *
     * @param bank Bank
     * @return Object
     */
    @At("/add")
    @Ok("json")
    @RequiresPermissions("sys.manager.bank.add")
    @SLog(tag = "新建BankCard", msg = "Bank名:${args[0].name}")
    public Object add(@Param("..") SysCollectBank bank) {
        try {
            bank.setOpAt(1L);
            if (!BankCardUtil.checkBankCard(bank.getCardNo())) {
                return Result.error("银行卡号格式错误");
            }
            bankService.insert(bank);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }


    /**
     * 根据id查询银行卡信息
     * @param id String
     * @return Object
     */
    @At("/edit/?")
    @Ok("json:full")
    public Object edit(@Param("id") String id) {
        try {
            return Result.success(bankService.fetch(id));
        } catch (Exception e) {
            return Result.error();
        }
    }

    /**
     * 修改银行卡信息
     * @param bank SysCollectBank
     * @return Object
     */
    @At("/editDo")
    @Ok("json")
    @RequiresPermissions("sys.manager.bank.edit")
    @SLog(tag = "修改银行卡", msg = "银行卡名:${args[1].getAttribute('name')}")
    public Object editDo(@Param("..") SysCollectBank bank) {
        try {
            bankService.update(bank);
            return Result.success();
        } catch (Exception e) {
            return Result.error();
        }
    }

    /**
     * 根据id启用银行卡
     *
     * @param id String
     * @return Object
     */
    @At("/enable/?")
    @Ok("json")
    @RequiresPermissions("sys.manager.bank.edit")
    @SLog(tag = "启用银行卡", msg = "银行卡名:${args[1].getAttribute('name')}")
    public Object enable(String id) {
        try {
            bankService.update(Chain.make("disabled", false), Cnd.where("id", "=", id));
            return Result.success();
        } catch (Exception e) {
            return Result.error();
        }
    }

    /**
     * 根据id禁用银行卡
     *
     * @param id String
     * @return Object
     */
    @At("/disable/?")
    @Ok("json")
    @RequiresPermissions("sys.manager.bank.edit")
    @SLog(tag = "禁用银行卡", msg = "银行卡名:${args[1].getAttribute('name')}")
    public Object disable(String id) {
        try {
            bankService.update(Chain.make("disabled", true), Cnd.where("id", "=", id));
            return Result.success();
        } catch (Exception e) {
            return Result.error();
        }
    }

    /**
     * 根据id删除银行卡
     * @param id String
     * @return Object
     */
    @At("/delete/?")
    @Ok("json")
    @RequiresPermissions("sys.manager.bank.delete")
    public Object delete(String id) {
        try {
            bankService.delete(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error();
        }

    }
}
